﻿using SqlSugar;

namespace DataCollection.Core.Models
{
    #region 属性
    /// <summary>
    /// 设备
    ///</summary>
    [SugarTable("c_device")]
    public class DeviceModel
    {
        ///// <summary>
        ///// 备  注:设备编号
        ///// 默认值:
        /////</summary>
        [SugarColumn(ColumnName = "number", IsPrimaryKey = true)]
        public long Number { get; set; }

        /// <summary>
        /// 备  注:通道地址
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "port_address")]
        public string PortAddress { get; set; } = null!;

        /// <summary>
        /// 备  注:设备名称
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "name")]
        public string Name { get; set; } = null!;

        /// <summary>
        /// 备  注:设备地址
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "slave_address")]
        public int SlaveAddress { get; set; }

        /// <summary>
        /// 备  注:波特率
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "baudrate")]
        public int Baudrate { get; set; }

        /// <summary>
        /// 备  注:DO点数量
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "coilstatus_count")]
        public int CoilStatusCount { get; set; }

        /// <summary>
        /// 备  注:DI点数量
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "inputstatus_count")]
        public int InputStatusCount { get; set; }

        /// <summary>
        /// 备  注:保持寄存器数量
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "holdingregister_count")]
        public int HoldingRegisterCount { get; set; }

        /// <summary>
        /// 备  注:输入寄存器数量
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "inputregister_count")]
        public int InputRegisterCount { get; set; }

        /// <summary>
        /// 备  注:设备状态
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "enable")]
        public string Enable { get; set; } = null!;

        [Navigate(NavigateType.OneToMany, nameof(DevicePointModel.DeviceNumber))]//BookA表中的studenId
        public List<DevicePointModel>? Points { get; set; }//注意禁止给books手动赋值

        #endregion

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public int Add()
        {
            List<DeviceModel> list = new List<DeviceModel>();
            list.Add(this);
            int res = SqlSugarHelper.Insertable<DeviceModel>(list);
            return res;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public int Delete()
        {
            List<DeviceModel> list = new List<DeviceModel>();
            list.Add(this);
            int res = SqlSugarHelper.Deleteable<DeviceModel>(list);
            return res;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <returns></returns>
        public int Update()
        {
            List<DeviceModel> list = new List<DeviceModel>();
            list.Add(this);
            int res = SqlSugarHelper.Updateable<DeviceModel>(list);
            return res;
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>

        static public List<DeviceModel> List(Dictionary<string, object> where) 
        {

            return SqlSugarHelper.Inquireable<DeviceModel>(where);
        }


        /// <summary>
        /// 更新
        /// </summary>
        /// <returns></returns>
        static public List<DeviceModel> PointList()
        {
           return SqlSugarHelper.DB.Queryable<DeviceModel>().Includes(x => x.Points).ToList();
        }
    }

}
